package com.example.yls.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_wifi;
    private Button btnStart;
    private Button btnStop;
    private AnimationDrawable animationDrawable;
    private ImageView iv_zyl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        iv_wifi=findViewById(R.id.iv_wifi);
        btnStart=findViewById(R.id.btn_start);
        btnStop=findViewById(R.id.btn_stop);
        iv_zyl=findViewById(R.id.iv_zyl);

        //1.利用 xml 实现逐帧动画
        // 1-1.获取 AnimationDrawable 对象
        animationDrawable= (AnimationDrawable) iv_wifi.getDrawable();

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1-2.开始动画
                //animationDrawable.start();
                //2.补间动画（淡入淡出：alpha；位移：translate；缩放：scale；旋转： rotate）
                //2-1.淡入淡出：alpha
                startAlphaAnim();
                //2-2.位移：translate
                startTransAnim();
                //2-3.缩放：scale
                stratScaleAnim();
                //2-4.旋转： rotate
                stratRotateAnim();
                //3-1.利用代码实现动画集合
                stratAnimationSetWithJava();
                //3-2.利用XML实现动画集合
                startAnimationSetWithXML();
            }
        });
        //1-3.停止动画
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animationDrawable.stop();
            }
        });
    }

    //利用XML实现动画集合
    private void startAnimationSetWithXML() {
        AnimationSet animationSet= (AnimationSet) AnimationUtils.loadAnimation
                (MainActivity.this,R.anim.set_anim);
        iv_zyl.startAnimation(animationSet);
    }

    //利用代码实现动画集合
    private void stratAnimationSetWithJava() {
        AnimationSet animationSet = new AnimationSet(true);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                0,400,0,600);
        RotateAnimation rotateAnimation=new RotateAnimation(
                0,360,180,180);
        ScaleAnimation scaleAnimation=new ScaleAnimation(
                1,3,1,4,180,180);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setDuration(3000);
        animationSet.setRepeatCount(5);
        animationSet.setRepeatMode(Animation.REVERSE);
        iv_zyl.startAnimation(animationSet);
    }
    //旋转： rotate
    private void stratRotateAnim() {
        /*//2-4-1.利用代码实现
        RotateAnimation rotateAnimation=new RotateAnimation(0,360,
                180,180);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setFillAfter(true);*/
        //2-4-2.利用 xml 实现
        RotateAnimation rotateAnimation= (RotateAnimation) AnimationUtils.loadAnimation
                (MainActivity.this,R.anim.rotate_anim);
        iv_zyl.startAnimation(rotateAnimation);
    }

    //缩放：scale
    private void stratScaleAnim() {
        //2-3-1.利用代码实现
        /*ScaleAnimation scaleAnimation=new ScaleAnimation(1,3,1,3,100,50);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setRepeatCount(5);
        scaleAnimation.setRepeatMode(Animation.REVERSE);*/
        //2-3-2.利用 xml 实现
        ScaleAnimation scaleAnimation= (ScaleAnimation) AnimationUtils.loadAnimation
                (MainActivity.this,R.anim.scale_anim);
        iv_zyl.startAnimation(scaleAnimation);
    }
    //位移：translate
    private void startTransAnim() {
        //2-2-1.利用代码实现
        /*TranslateAnimation translateAnimation = new TranslateAnimation
                (0,300,0,300);
        translateAnimation.setDuration(3000);
        translateAnimation.setFillAfter(true);
        translateAnimation.setRepeatCount(-1);
        translateAnimation.setRepeatMode(Animation.REVERSE);*/
        //2-2-2.利用 xml 实现
        TranslateAnimation translateAnimation= (TranslateAnimation) AnimationUtils.loadAnimation
                (MainActivity.this,R.anim.translate_anim);
        iv_zyl.startAnimation(translateAnimation);
    }
    //透明度动画alpha
    private void startAlphaAnim() {
        /*//2-1-1.利用 xml 实现
        AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation
                (MainActivity.this,R.anim.alpah_anim);*/

        //2-1-2.利用代码实现
        AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0.0f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setRepeatCount(3);
        alphaAnimation.setRepeatMode(Animation.REVERSE);

        iv_zyl.startAnimation(alphaAnimation);
    }
}
